import json
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import Column, String

Base = declarative_base()


class Mov(Base):
    __tablename__ = 'move'
    name = Column(String(100), primary_key=True)
    types = Column(String(100))
    t = Column(String(20))


class OrmDB:
    def __init__(self):
        sql = 'mysql+pymysql://root:112358@localhost:3306/homework?charset=utf8'
        engine = create_engine(sql, max_overflow=5)
        maker = sessionmaker(bind=engine)
        self.session = maker()

    def add_seacher(self, ls):
            obj1 = Mov()
            obj1.name = ls[0]
            obj1.types = ls[1]
            obj1.t = ls[2]
            self.session.add(obj1)
            self.session.commit()


if __name__ == '__main__':
    db = OrmDB()
    with open('./in_theaters.json','r') as f:
        load = json.load(f)
        resluts = load['subjects']
        for items in resluts:
            ls=[]
            ls.append(items['title'])
            str = ','.join(items['genres'])
            ls.append(str)
            ls.append(items['mainland_pubdate'])
            db.add_seacher(ls)
        # ls = ['碟中谍6：全面瓦解', '动作,惊悚,冒险', '2018-08-31']
        # db.add_seacher(ls)




